<?php
/**
 * 发送邮件类
 * @package core
 * @subpackage class
 * @author
 * @version $Id$
 */
//预先包含文件

include_once(dirname(__FILE__)."/../htmlMimeMail5/htmlMimeMail5.php");
class Mail extends Application {
	/* 成员变量 */
	private $_mail_handler = null;

	private $_from_address = '';
	private $_from_name = '';
	// 错误信息
	public $_error = '';
	// 发送信件服务器信息
	private $_smtp_server = array();
	// 接收信件服务器信息
	private $_pop_server = array();
	/**
	 * 构造函数
	 *
	 */
	public function __construct($mail_config){
		$this->_mail_handler = new htmlMimeMail5();
		$this->_mail_handler->setHeadCharset('utf-8');
		$this->_mail_handler->setTextCharset('utf-8');
		$this->_mail_handler->setHtmlCharset('utf-8');
		if ( !isset($mail_config['address']) ){
			exit('邮件配置未提供address');
		}
		if ( !isset($mail_config['name']) ){
			exit('邮件配置未提供name');
		}
		if ( !isset($mail_config['smtp']) ){
			exit('邮件配置未提供smtp');
		}
		if ( !isset($mail_config['pop']) ){
			exit('邮件配置未提供pop');
		}
		$this->_from_address = $mail_config['address'];
		$this->_from_name = $mail_config['name'];
		$this->_smtp_server = $mail_config['smtp'];
		$this->_pop_server = $mail_config['pop'];
	}

	/**
	 * 设置邮件任务
	 *
	 * @param string or array $to_email		收信人列表
	 * @param string $subject				信件标题
	 * @param string $html_content			内容
	 * @param string $text_content			文字内容
	 * @param string $from_name				发送人姓名
	 * @param string $from_email			发送人邮件
	 */
	public static function setTask($to_email,$subject,$html_content,$text_content='',$from_name='',$from_email='',$weigth=0){
		$db = new Happy_db();
		$to_email_list = array();
		if ( is_string($to_email) ){
			$to_email_list[] = $to_email;
		}
		elseif ( is_array($to_email) ){
			$to_email_list = $to_email;
		}
		if ( empty($to_email_list) ){
			return false;
		}
		$mail_info = array();
		$mail_info['from_name'] = trim($from_name);
		$mail_info['from_email'] = trim($from_email);
		$mail_info['subject'] = trim($subject);
		$mail_info['text_content'] = trim($text_content);
		$mail_info['html_content'] = trim($html_content);
		$mail_info['weight'] = intval($weigth);
		$mail_info['create_time'] = date("Y-m-d H:i:s");
		foreach ($to_email_list as $mail){
			if ( !is_string($mail) ){
				continue;
			}
			$mail_info['to_email'] = trim($mail);
			$new_mail_info = array_map('mysql_escape',$mail_info);
			$sql = "INSERT INTO `core_email`(`".implode("`,`",array_keys($new_mail_info))."`)
					VALUES('".implode("','",array_values($new_mail_info))."')";
			$db->execute($sql);
		}
		return true;
	}

	/**
	 * 发送邮件
	 *
	 * @param array	to_email 接收人
	 * @param string subject 主题
	 * @param string $html_content HTML内容
	 * @param string $text_content Text内容
	 * @return bool
	 */
	public function sendMail($to_email,$subject,$html_content,$text_content='',$from=''){
		if ( empty($subject) ){
			$this->_error = '未提供邮件主题';
			return false;
		}
		if ( empty($to_email) ){
			$this->_error = '未提供邮件接收人';
			return false;
		}
		/*
		if ( $from ){
			$this->_mail_handler->setFrom($from);
		}
		else {
			$this->_mail_handler->setFrom($this->_from_name.'<'.$this->_from_address.'>');
		}
		*/
		$this->_mail_handler->setFrom($this->_from_name.'<'.$this->_from_address.'>');
		$this->_mail_handler->setSMTPParams($this->_smtp_server['host'], $this->_smtp_server['port'], $this->_smtp_server['halo'] , $this->_smtp_server['auth'], $this->_smtp_server['user'], $this->_smtp_server['pass']);
		$this->_mail_handler->setSubject($subject);

		if ( $text_content!='' ){
			$this->_mail_handler->setText($text_content);
		}
		$this->_mail_handler->setHTML($html_content);
		if ( !is_array($to_email) ){
			$to_email = array($to_email);
		}
		$result = $this->_mail_handler->send($to_email, 'smtp');
		if ( $result ){
			return true;
		}
		else {
			$this->_error = $this->_mail_handler->errors;
			return false;
		}
	}
}
//类定义结束
?>